package com.senven.api.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senven.api.commom.ResponsePage;
import com.senven.api.dao.AccountMapper;
import com.senven.api.dao.RoleMapper;
import com.senven.api.pojo.Account;
import com.senven.api.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author LQP
 */
@Service
public class AccountService implements UserDetailsService {
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        //获取账户信息
        QueryWrapper<Account> wrapper = new QueryWrapper<Account>();
        wrapper.eq("account",s);
        Account account = accountMapper.selectOne(wrapper);
        System.out.println(account.getPassword());
        if(account == null){
            throw new UsernameNotFoundException("账户不存在");
        }

        //获取角色ID
        int id = account.getRoleid();
        QueryWrapper<Role> rwrapper = new QueryWrapper<Role>();
        rwrapper.eq("id",id);

        //设置角色
        account.setRole(roleMapper.selectOne(rwrapper));
        return account;
    }

    //获取管理员信息
    public ResponsePage<Account> queryAccount(String s){
        QueryWrapper<Account> wrapper = new QueryWrapper<Account>();
        System.out.println("传入名字:"+s);
        wrapper.eq("account",s);
        Account account = accountMapper.selectOne(wrapper);
        if (account == null){
            ResponsePage<Account> responsePage= new ResponsePage<Account>();
            responsePage.setTotal(10);
            responsePage.setCode(0);
            responsePage.setCurrent(1);
            List<Account> l = new LinkedList<Account>();
            responsePage.setData(l);
            return responsePage;
        }
        //获取角色ID
        int id = account.getRoleid();
        QueryWrapper<Role> rwrapper = new QueryWrapper<Role>();
        rwrapper.eq("id",id);
        //设置角色
        account.setRole(roleMapper.selectOne(rwrapper));

        ResponsePage<Account> responsePage= new ResponsePage<Account>();
        responsePage.setTotal(10);
        responsePage.setCode(0);
        responsePage.setCurrent(1);
        List<Account> l = new LinkedList<Account>();
        l.add(account);
        responsePage.setData(l);

        return responsePage;
    }

    /**
     * ID获取管理员信息
     * @param id
     * @return
     */
    public Account queryAccountById(int id){
        return accountMapper.selectById(id);
    }


    /**
     * 获取管理员列表
     * @param current
     * @param size
     * @return
     */
    public ResponsePage<Account> queryAccountList(int current, int size){
        Page<Account> page = new Page<Account>(current, size);

        IPage<Account> iPage = accountMapper.selectPage(page,null);
        ResponsePage<Account> responsePage= new ResponsePage<Account>();
        responsePage.makePage(iPage);

        return responsePage;
    }

    /**
     * 插入管理员
     * @param account
     * @return
     */
    public int insertAccount(Account account){
        account.setCreateTime(new Date());
        account.setModifyTime(new Date());
        int result = accountMapper.insert(account);
        return result;
    }

    /**
     * 删除管理员
     * @param id
     * @return
     */
    public int deleteAccount(int id){
        int result = accountMapper.deleteById(id);
        return result;
    }

    /**
     * 更新管理员
     * @param account
     * @return
     */
    public int updateAccount(Account account){
        QueryWrapper<Account> wrapper = new QueryWrapper<Account>();
        wrapper.eq("id",account.getId());
        account.setModifyTime(new Date());
        int result = accountMapper.update(account,wrapper);
        return result;
    }

    /**
     * 重置密码
     * @param oldpass
     * @param newpass
     * @param account
     */
    public Boolean resetPasswd(String oldpass,String newpass,String account){
        QueryWrapper<Account> wrapper = new QueryWrapper<Account>();
        wrapper.eq("account",account);
        Account account1 = accountMapper.selectOne(wrapper);

        //BCryptPasswordEncoder bcryptPasswordEncoder = new BCryptPasswordEncoder();
        PasswordEncoder password = new BCryptPasswordEncoder();
        if(  password.matches(oldpass, account1.getPassword())) {
            account1.setPassword(password.encode(newpass));
            accountMapper.updateById(account1);
            return true;
        }else {
            return false;
        }

    }
}
